package com.ruoyi.web.controller;

import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.web.domain.Area;
import com.ruoyi.web.domain.dto.AreaDto;
import com.ruoyi.web.service.AreaService;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/area")
public class AreaController extends BaseController {

    @Autowired
    private AreaService areaService;

    @ApiOperation("获取省市区")
    @GetMapping("/getArea")
    public AjaxResult getArea(AreaDto areaDto){
        Area area = new Area();
        BeanUtils.copyProperties(areaDto, area);
        if(StringUtils.isNotBlank(areaDto.getShanDongFlag()) && StringUtils.equals(areaDto.getShanDongFlag(), "山东")) {
            area.setBaseName("山东省");
        }
        List<Area> list = areaService.findListPage(null, area);
        return AjaxResult.success(list);
    }

    @ApiOperation("调用阿里云获取地图数据API")
    @GetMapping("/getGeoJson")
    public AjaxResult getGeoJson(@RequestParam("areaCode") String areaCode){
        JSONObject geoJson = areaService.getGeoJson(areaCode);
        return AjaxResult.success(geoJson);
    }
}
